package com.session;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginPage
 */
@WebServlet("/LoginPage")
public class LoginPage extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LoginPage() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected   void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
String user=request.getParameter("user");
String password=request.getParameter("pass");

if(user.equals("admin") && password.equals("pass1") ){

	   HttpSession session=request.getSession(); 
	  // session.setMaxInactiveInterval(1);  setting session time programmatically.
	    System.out.println("New Session "+session.isNew());
	    ActiveUserDetails activeUserDetails=new ActiveUserDetails();
	    activeUserDetails.setUsername("Demo name");
	    activeUserDetails.setUseId("007");
	    session.setAttribute("activeUser", activeUserDetails);
	    System.out.println("Last Access time:"+session.getLastAccessedTime());
	    System.out.println("creation time"+ session.getCreationTime());
	   session.setAttribute("userName", user);
	   
	   Cookie  cookie=new Cookie("userLoginInfo", new Date(System.currentTimeMillis()).toString());
	   cookie.setMaxAge(1);
	   
	   response.addCookie(cookie);
	   String url=response.encodeURL("WelcomeServlet");
	   System.out.println(url);
	   response.sendRedirect(url);; //302 
}
	
	

	}

}
